package com.itheima.reggie.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author mff
 * @date 2022/6/12 14:39
 * @mood happy
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user,HttpSession session){
//      接收手机号
        String phone = user.getPhone();
//      生成验证码
        String code = RandomUtil.randomNumbers(6);
//      调用工具类发送短信
//        发送
//        SMSUtils2.sendSms(user.getPhone(),code);
//        存储到session ->优化，存储到redis中
//        session.setAttribute(phone,code);
//        redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set(phone,code,5,TimeUnit.MINUTES);
        String s = (String) redisTemplate.opsForValue().get(phone);
        System.out.println(s);
        System.out.println(code);
        return R.success("发送成功");
    }

    @PostMapping("/login")
    public R<String> login(@RequestBody Map<String,String> map,HttpSession session){
//        获取手机号，用户输入的验证码
        String phone = map.get("phone");
        String code = map.get("code");
        if (StringUtils.isBlank(phone)||StringUtils.isBlank(code)){
            return R.error("用户名或验证码不能为空");
        }
//      获取生成的验证码用来比对 -> 优化在redis中获取
//        String codeInsession = (String) session.getAttribute(phone);

        String codeInsession = (String)redisTemplate.opsForValue().get(phone);
        System.out.println(codeInsession);
        if (codeInsession == null){
            return R.error("验证码已过期，请重新获取");
        }
        if (codeInsession.equals(code)){
//            存入session，保证过滤器不会过滤
//            判断是不是新用户
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getPhone, phone);
            User one = userService.getOne(userLambdaQueryWrapper);
            session.setAttribute("user",one);
            if (one==null){
                User user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                session.setAttribute("newUser",user);
                userService.save(user);
            }
//            比对完之后清除redis中的验证码
            redisTemplate.delete("phone");
            return R.success("登录成功");
        }
        return R.error("登录失败");
    }

    @PostMapping("/loginout")
    public R<String> loginOut(HttpSession session){

        if (session.getAttribute("user")!=null){
            session.removeAttribute("user");
        }
        if (session.getAttribute("newUser")!=null){
            session.removeAttribute("newUser");
        }
        return R.success("您已退出登录");
    }

}
